package com.vogtec.fota.controller;

import com.vogtec.fota.entity.FirmWare.*;
import com.vogtec.fota.service.FirmWareService;
import lombok.extern.slf4j.Slf4j;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

/**
 * Created by wei on 2017/7/3.
 */
@Slf4j
@RestController
@RequestMapping("/sharingbike/v1.0/upgrade/fota/firmware")
public class FirmWareController {

    @Autowired
    private FirmWareService firmWareService;

    /**
     * 新建固件
     * @param
     * @return
     */
    @GetMapping(value = "/token")
    public Map<String,String> addFirmWare(/*@RequestBody AddFirmWare afw*/){
        log.info("<<== AddFirmWare "/* + afw.toString()*/);
        AddFirmWareResult afwr = firmWareService.addFirmWare(/*afw*/);
        log.info(">>== AddFirmWareResult: " + afwr.toString());
        Map<String,String> map = new HashMap<>();
        map.put("uptoken", afwr.getUptoken());
        return map;
    }

    /**
     * 根据id查询固件
     * @param id
     * @return
     */
    @GetMapping(value = "/{id}")
    public FWQueryResult fwQuery(@PathVariable Integer id){
        log.info("<<== id: " + id);
        FWQueryResult fwqr = firmWareService.fwQuery(id);
        log.info(">>== FWQueryResult: " + fwqr.toString());

        return fwqr;
    }

    /**
     * 查询所有固件
     * @param pageNo
     * @param pageSize
     * @return
     */
    @GetMapping(value = "")
    public FWQueryAllResult fwQueryAll(@RequestParam(value= "pageNo",required = false,defaultValue = "1") Integer pageNo,
                                       @RequestParam(value = "pageSize",required = false,defaultValue = "0") Integer pageSize){
        log.info("<<== pageNo: " + pageNo + ", pageSize: " + pageSize);
        FWQueryAllResult fwqar = firmWareService.fwQueryAll(pageNo,pageSize);
        log.info(">>== FWQueryAllResult: " + fwqar.toString());
        return fwqar;
    }

    /**
     * 删除固件
     * @param id
     * @return
     */
    @DeleteMapping(value = "/{id}")
    public DeleteFWResult deleteFW(@PathVariable Integer id){
        log.info("<<== id: " + id);
        DeleteFWResult dfwr = firmWareService.deleteFW(id);
        log.info(">>== DeleteFWResult: " + dfwr.toString());
        return dfwr;
    }

    /**
     * 获取token后回调notify（供页面callback）
     * @param afw
     */
    @PostMapping(value = "/notify",
            consumes = {"application/json"})
    public void notifyToken(@RequestBody AddFirmWare afw){
        log.info("<<== firmWareName: " + afw.getFirmWareName());
        firmWareService.notifyToken(afw);
    }

    /**
     * 7牛notify（供7niu callback 不做处理）
     */
    @PostMapping(value = "/7niunotify")
    public void notifyToken(){
        log.info("7niu Notify  7niu Notify  7niu Notify");
    }

}
